﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Pavilion
{
    public class CTexture2D
    {
        private Texture2D currentTexture;
        private ContentManager content;

        public bool Visible { get; set; }
        public bool EnableLoading { get; set; }

        public event Action<Texture> TextureItemChanged;

        public CTexture2D()
        {
            content = new ContentManager(GameEngine.Services);
            Visible = true;
            EnableLoading = true;
        }

        public CTexture2D(Texture2D texture) : this()
        {
            this.TextureItem = texture;
        }

        public CTexture2D Clone()
        {
            byte[] textureData = new byte[Width * Height * 4];
            currentTexture.GetData<byte>(textureData);

            return new CTexture2D(CreateTexture(Width, Height, textureData))
            {
                Visible = this.Visible,
                EnableLoading = this.EnableLoading
            };
        }

        public void Load(string filePath, string fileName)
        {
            if (EnableLoading)
            {
                content.RootDirectory = filePath;
                TextureItem = content.Load<Texture2D>(fileName);
            }
        }

        public void Unload()
        {
            if (EnableLoading)
            {
                content.Unload();
                TextureItem = null;
            }
        }

        public void Draw(Rectangle destinationRectangle, Color color)
        {
            if (!Visible)
                return;

            DrawManager.Instance.Draw(TextureItem, destinationRectangle, color);
        }

        public void Draw(Vector2 position, Color color)
        {
            if (!Visible)
                return;

            DrawManager.Instance.Draw(TextureItem, position, color);
        }

        public void Draw(Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
        {
            if (!Visible)
                return;

            DrawManager.Instance.Draw(TextureItem, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth);
        }

        public void DrawOnScreen(Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
        {
            if (!Visible)
                return;

            DrawManager.Instance.DrawOnScreen(TextureItem, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth);
        }

        /// <summary>
        /// Gets the width of this texture resource, in pixels.
        /// </summary>
        public int Width
        {
            get
            {
                if (TextureItem == null)
                    return 0;
                
                return TextureItem.Width;
            }
        }

        /// <summary>
        /// Gets the height of this texture resource, in pixels.
        /// </summary>
        public int Height
        {
            get
            {
                if (TextureItem == null)
                    return 0;

                return TextureItem.Height;
            }
        }

        /// <summary>
        /// Gets or sets the texture item of this wrapper.
        /// </summary>
        public Texture2D TextureItem
        {
            get { return currentTexture; }
            set
            {
                if (currentTexture == value)
                    return;

                currentTexture = value;

                OnTextureItemChanged();
            }
        }

        private void OnTextureItemChanged()
        {
            if (TextureItemChanged != null)
                TextureItemChanged(TextureItem);
        }

        public static Texture2D CreateTexture(int width, int height)
        {
            return new Texture2D(DrawManager.Instance.Graphics, width, height);
        }

        public static Texture2D CreateTexture(int width, int height, byte[] textureData)
        {
            Texture2D texture = CreateTexture(width, height);
            texture.SetData<byte>(textureData);

            return texture;
        }
    }
}
